package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.SetmealMapper;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service(interfaceClass = SetmealService.class)
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public boolean updateSetmeal(Setmeal setmeal, List<Integer> checkGroupIds) {
        Setmeal byId = setmealMapper.findById(setmeal.getId());
        //根据套餐id查询检查组id
        List<Integer> checkGroupIdBySetmeal = this.setmealMapper.findCheckGroupIdsBySetmealId(setmeal.getId());

        //根据后端查询到的检查组id得到需要添加的检查组id
        checkGroupIds.removeAll(checkGroupIdBySetmeal);
        if (checkGroupIds.size() <= 0) {
            return false;
        }

        //添加检查组id到套餐与检查组的关系表
        int i1 = this.setmealMapper.addSetmealAndCheckGroup(setmeal.getId(), checkGroupIds);

        //根据前端传来的数据得到需要删除的检查组id
        checkGroupIds.addAll(checkGroupIdBySetmeal);
        checkGroupIdBySetmeal.removeAll(checkGroupIds);
        if (checkGroupIdBySetmeal.size() <= 0) {
            return false;
        }
        //将需要删除的检查组id从套餐与检查组表移除
        int i = this.setmealMapper.delSetmealAndCheckGroup(checkGroupIdBySetmeal);
        boolean b = setmealMapper.updateSetmeal(setmeal);
        return b;
    }

    /**
     * 根据套餐id查询
     * @param setmealId
     * @return
     */
    @Override
    public Setmeal findById(Integer setmealId) {
        Setmeal byId = setmealMapper.findById(setmealId);
        return byId;
    }

    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> all = setmealMapper.findAll();
        return all;
    }

    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer setmealId) {
        List<Integer> checkGroupIds = setmealMapper.findCheckGroupIdsBySetmealId(setmealId);
        return checkGroupIds;
    }

    @Override
    public Result addSetmeal(Setmeal setmeal, List<Integer> checkgroupIds) {
        final Boolean b = setmealMapper.addSetmeal(setmeal);
        final Integer code = setmeal.getId();
        if (b) {
            final int i = setmealMapper.addSetmealAndCheckGroup(code, checkgroupIds);
            if (i > 0) {
                return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
            }
        }

        return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
    }

    @Override
    public Result deleteSetmeal(Integer id, List<Integer> checkgroupIds) {
        final Boolean b = setmealMapper.deleteSetmeal(id);
        if (b) {
            final int i = setmealMapper.delSetmealAndCheckGroup(checkgroupIds);
            if (i > 0){
                return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
            }
        }
        return new Result(true,MessageConstant.DELETE_SETMEAL_FAIL);
    }
}
